package com.yida.gwj.report.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.report.domain.dto.GwjReportDepositBusinessDTO;
import com.yida.gwj.report.domain.request.GwjReportDepositBusinessQueryRequest;
import com.yida.gwj.report.domain.vo.GwjReportDepositBusinessDetailExcelVO;
import com.yida.gwj.report.domain.vo.GwjReportDepositBusinessExcelVO;
import com.yida.gwj.report.service.IGwjReportDepositBusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lqq
 * @date 2023-03-06
 */
@RestController
@RequestMapping("/deposit")
@Api(tags = "缴存业务统计报表")
public class GwjReportDepositBusinessController extends BaseController {

    @Resource
    private IGwjReportDepositBusinessService gwjReportDepositBusinessService;

    @ApiOperation(value = "统计所有缴存业务（无需分页）", response = GwjReportDepositBusinessDTO.class)
    @RequiresPermissions("gwj:report:deposit:list")
    @GetMapping("/statisticsAll")
    public AjaxResult statisticsAll(GwjReportDepositBusinessQueryRequest request) {
        return AjaxResult.success(gwjReportDepositBusinessService.statisticsAllDepositBusiness(request));
    }

    @ApiOperation("统计单个缴存业务（需分页）")
    @RequiresPermissions("gwj:report:deposit:list")
    @GetMapping("/statisticsOne")
    public TableDataInfo statisticsOne(GwjReportDepositBusinessQueryRequest request) {
        if (StringUtils.isNull(request.getBizFlagId())) {
            return getDataTable(new ArrayList<GwjReportDepositBusinessDTO>());
        }
        startPage();
        List<GwjReportDepositBusinessDTO> list = gwjReportDepositBusinessService.statisticsOneDepositBusiness(request);
        return getDataTable(list);
    }

    @PostMapping("/export")
    @RequiresPermissions("gwj:report:deposit:export")
    @Log(title = "导出缴存信息", businessType = BusinessType.EXPORT, systemName = "gwj")
    @ApiOperation("导出缴存信息")
    public void export(HttpServletResponse response, @RequestBody GwjReportDepositBusinessQueryRequest request) {
        List<GwjReportDepositBusinessDTO> list = gwjReportDepositBusinessService.statisticsAllDepositBusiness(request);
        if (StringUtils.isEmpty(list)) {
            throw new ServiceException("查无数据");
        }
        ExcelUtil<GwjReportDepositBusinessExcelVO> util = new ExcelUtil<>(GwjReportDepositBusinessExcelVO.class);
        util.exportExcel(response, BeanUtil.copyToList(list, GwjReportDepositBusinessExcelVO.class), "缴存信息");
    }

    @PostMapping("/exportDetail")
    @RequiresPermissions("gwj:report:deposit:export")
    @Log(title = "导出各小区缴存信息", businessType = BusinessType.EXPORT, systemName = "gwj")
    @ApiOperation("导出各小区缴存信息")
    public void exportDetail(HttpServletResponse response, @RequestBody GwjReportDepositBusinessQueryRequest request) {
        if (StringUtils.isNull(request.getBizFlagId())) {
            throw new ServiceException("请选择一条业务");
        }
        List<GwjReportDepositBusinessDTO> list = gwjReportDepositBusinessService.statisticsOneDepositBusiness(request);
        if (StringUtils.isEmpty(list)) {
            throw new ServiceException("查无数据");
        }
        ExcelUtil<GwjReportDepositBusinessDetailExcelVO> util = new ExcelUtil<>(GwjReportDepositBusinessDetailExcelVO.class);
        util.exportExcel(response, BeanUtil.copyToList(list, GwjReportDepositBusinessDetailExcelVO.class), "缴存信息");
    }
}
